package com.hongxun.budget.ui.dialog;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.utils.T;
import com.hongxun.greenentity.tk.BMaterialEntity;
import com.hongxun.greenentity.tk.BOtherQuantitiesEntity;
import com.hongxun.interfaces.ResponseListener;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by admin on 2018/9/5.
 */

public class MaterialInputDialog extends BaseCenterFragmentDialog {

    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.type)
    TextView type;
    @BindView(R.id.parameter)
    TextView parameter;
    @BindView(R.id.count)
    EditText count;
    @BindView(R.id.unit)
    TextView unit;

    BMaterialEntity b;
    BOtherQuantitiesEntity q;
    ResponseListener listener;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_materil_input;
    }

    @Override
    protected void initViews() {
        if (b != null) {
            name.setText(b.getMName());
            type.setText(b.getMLabel());
            parameter.setText(b.getMBParameter());
            unit.setText(b.getMUnit());
        } else if (q != null) {
            name.setText(q.getOQName());
            type.setText(q.getOQWork());
            parameter.setText(q.getOQParameter());
            unit.setText(q.getOQMeterUnit());
        }
    }

    public MaterialInputDialog setB(BMaterialEntity b) {
        this.b = b;
        return this;
    }

    public MaterialInputDialog setQ(BOtherQuantitiesEntity q) {
        this.q = q;
        return this;
    }

    public MaterialInputDialog setListener(ResponseListener listener) {
        this.listener = listener;
        return this;
    }

    @OnClick({R.id.cancel, R.id.confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                break;
            case R.id.confirm:
                String c = count.getText().toString();
                if (TextUtils.isEmpty(c) || Double.parseDouble(c) == 0) {
                    T.showLong(context, "数量不能为空或0");
                    return;
                }
                if (b != null) {
                    b.setmCount(Double.parseDouble(c));
                    listener.response(b);
                } else if (q != null) {
                    q.setCount(Double.parseDouble(c));
                    listener.response(q);
                }
                break;
        }
        dismiss();
    }
}
